package com.iflyun.rabbitmq.main;

import java.util.Map;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.iflyun.rabbitmq.bean.Result;
import com.iflyun.rabbitmq.common.Common;
import com.iflyun.rabbitmq.utils.MailUtils;
import com.iflyun.rabbitmq.utils.RabbitmqUtils;

@Component
public class Monitor {

	@Scheduled(cron = "0 0 0/1 * * ? ") // 每一个小时执行一次
	public void rabbitMQCustomer() {
		int ready = 0;
		int unacknowledged = 0;
		int auto_delete = 0;
		int durable = 0;
		Map<String, Integer> map = RabbitmqUtils.CheckCustomer("Weblog");
		ready = map.get("ready");
		unacknowledged = map.get("unacknowledged");
		auto_delete = map.get("auto_delete");
		durable = map.get("durable");
		if (ready > Common.Error || unacknowledged > Common.Error) {
			MailUtils.Send(Common.prefix + "consumption Exception: accumulation value of more than 5000 ",
					"941911337@qq.com,wenquxing@iflyun.com");
		} else if (ready > Common.Warn || unacknowledged > Common.Warn) {
			MailUtils.Send(Common.prefix + "consumption Warning: second accumulation value of more than 2000",
					"941911337@qq.com,wenquxing@iflyun.com");
		}
		if (auto_delete != 1) {
			MailUtils.Send(Common.prefix + "Queue Weblog-auto_delete flag is not false",
					"941911337@qq.com,wenquxing@iflyun.com");

		}
		if (durable != 0) {
			MailUtils.Send(Common.prefix + "Queue Weblog-durable flag is not true",
					"941911337@qq.com,wenquxing@iflyun.com");

		}

	}

	@Scheduled(cron = "0 0 0/1 * * ? ") // 每一小时执行一次
	public void rabbitMQ() {
		Result result = RabbitmqUtils.CheckQueue("Weblog");
		if (!"200".equals(result.getStatus())) {
			MailUtils.Send(Common.prefix + result.getMsg(), "941911337@qq.com,wenquxing@iflyun.com");
		}

	}

	@Scheduled(cron = "0 0 7 1/1 * ? ") // 每天7点执行一次
	public void TestMQ() {
		int ready = 0;
		int unacknowledged = 0;
		int auto_delete = 0;
		int durable = 0;
		Map<String, Integer> map = RabbitmqUtils.CheckCustomer("Weblog");
		ready = map.get("ready");
		unacknowledged = map.get("unacknowledged");
		auto_delete = map.get("auto_delete");
		durable = map.get("durable");
		if (ready > Common.Error || unacknowledged > Common.Error) {
			MailUtils.Send(Common.prefix + "consumption Exception: accumulation value of more than 5000 ",
					"941911337@qq.com,wenquxing@iflyun.com");
		} else if (ready > Common.Warn || unacknowledged > Common.Warn) {
			MailUtils.Send(Common.prefix + "consumption Warning: second accumulation value of more than 2000",
					"941911337@qq.com,wenquxing@iflyun.com");
		}
		if (auto_delete != 1) {
			MailUtils.Send(Common.prefix + "Queue Weblog-auto_delete flag is not false",
					"941911337@qq.com,wenquxing@iflyun.com");

		}
		if (durable != 0) {
			MailUtils.Send(Common.prefix + "Queue Weblog-durable flag is not true",
					"941911337@qq.com,wenquxing@iflyun.com");

		} else {
			MailUtils.Send(Common.prefix + "consumption Normal", "941911337@qq.com,wenquxing@iflyun.com");
		}
		Result result = RabbitmqUtils.CheckQueue("Weblog");
		MailUtils.Send(Common.prefix + result.getMsg(), "941911337@qq.com,wenquxing@iflyun.com");

	}

}
